/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.prabhu.jeazyprops.validation;

import com.prabhu.jeazyprops.props.BaseProps;
import java.awt.Color;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;


/*
 * @author prabhu.p
 */
public class Validator
        extends InputVerifier
{

    BaseProps lProps;

    public Validator( BaseProps pProps )
    {
        lProps = pProps;
    }

    @Override
    public boolean shouldYieldFocus( JComponent input )
    {
        return super.shouldYieldFocus( input );
    }

    @Override
    public boolean verify( JComponent input )
    {
        boolean lReturn = false;
        if ( input instanceof JTextField )
        {
            String lField = input.getAccessibleContext().getAccessibleName();
            String datatype = lProps.getDatatype( lField );
            lReturn = CheckIsValid( ((JTextField) input).getText(), datatype );
            ChangeIndicatior( lReturn, input );
        }
        else if ( input instanceof JPasswordField )
        {
            String lField = input.getAccessibleContext().getAccessibleName();
            String datatype = lProps.getDatatype( lField );
            lReturn = CheckIsValid( ((JPasswordField) input).getPassword().
                    toString(), datatype );
            ChangeIndicatior( lReturn, input );
        }
        else
        {
            lReturn = true;
        }
        return lReturn;
    }

    public void ChangeIndicatior( boolean lReturn, JComponent input )
    {
        if ( input instanceof JTextField )
        {
            Highlighter highlighter = ((JTextField) input).getHighlighter();
            highlighter.removeAllHighlights();
            if ( !lReturn )
            {
                try
                {
                    highlighter.addHighlight( 0, ((JTextField) input).getText().
                            length(),
                                              new DefaultHighlighter.DefaultHighlightPainter(
                            Color.PINK ) );
                }
                catch ( BadLocationException ex )
                {
                    Logger.getLogger( Validator.class.getName() ).log(
                            Level.SEVERE, null, ex );
                }
            }
        }
        else if ( input instanceof JPasswordField )
        {
            Highlighter highlighter = ((JPasswordField) input).getHighlighter();
            highlighter.removeAllHighlights();
            if ( !lReturn )
            {
                try
                {
                    highlighter.addHighlight( 0, ((JPasswordField) input).
                            getPassword().length,
                                              new DefaultHighlighter.DefaultHighlightPainter(
                            Color.PINK ) );
                }
                catch ( BadLocationException ex )
                {
                    Logger.getLogger( Validator.class.getName() ).log(
                            Level.SEVERE, null, ex );
                }
            }
        }
    }

    private boolean CheckIsValid( String text, String lType )
    {
        if ( lType.equals( "int" ) || lType.equals( "java.lang.Integer" ) )
        {
            return Pattern.compile( "^(-)?(\\d){1,9}$" ).matcher( text ).matches();
        }
        else if ( lType.equals( "double" ) || lType.equals( "java.lang.Double" )
                || lType.equals( "float" ) || lType.equals( "java.lang.Float" ) )
        {
            return Pattern.compile( "^(-)?(\\d){1,10}\\.(\\d){1,10}$" ).matcher(
                    text ).matches();
        }
        else if ( lType.equals( "java.lang.String" ) )
        {
            if ( !text.isEmpty() )
            {
                return Pattern.compile( "^(\\S)(.){1,75}(\\S)$" ).matcher( text ).
                        matches();
            }
            return true;
        }
        return false;
    }
}
